/******************************************************************************
 * (C) 成都齐乐科技有限公司
 * 描述：
 *****************************************************************************/

module.exports = function (app) {
    return new TaskHandler(app);
};

function TaskHandler(app) {
    this.app = app;
}

TaskHandler.prototype.query = function(msg, session, next) {
    var self = this;
    var uid = session.uid;

    gPlayerManger.getPlayer(uid, function (player) {
        if (!player) {
            next(null, {code: Code.UID_INVALID});
            return;
        }

        gTaskManager.query(player, function(err, tasks){
            if (err) {
                next(null, {code: Code.SYSTEM_ERROR});
                return;
            }

            next(null, {tasks: tasks});
        });
    });
};

TaskHandler.prototype.update = function(msg, session, next) {
    var self = this;
    var uid = session.uid;
    var taskId = msg.taskId;
    var finishedCount = msg.finishedCount;

    gPlayerManger.getPlayer(uid, function (player) {
        if (!player) {
            next(null, {code: Code.UID_INVALID});
            return;
        }

        gTaskManager.update(player, {
            taskId: taskId,
            finishedCount: finishedCount
        }, function(err){
            next(null, {});
        });
    });
};

TaskHandler.prototype.getReward = function(msg, session, next) {
    var self = this;
    var uid = session.uid;
    var taskId = msg.taskId;

    gPlayerManger.getPlayer(uid, function (player) {
        if (!player) {
            next(null, {code: Code.UID_INVALID});
            return;
        }

        gTaskManager.getReward(player, taskId, function(code){
            next(null, {code: code});
        });
    });
};